#
# qflow project source compile makefile
#

# Main compiler arguments
CFLAGS = @CFLAGS@
LIBS = @LIBS@
LDFLAGS = @LDFLAGS@
INSTALL = @INSTALL@

prefix = @prefix@
exec_prefix = @exec_prefix@

DEFS = @DEFS@ -DQFLOW_VERSION=\"@VERSION@\" -DQFLOW_REVISION=\"@REVISION@\"

QFLOW_LIB_DIR = @QFLOW_LIB_DIR@

QFLOW_GRAYWOLF_PATH = @QFLOW_GRAYWOLF_PATH@
QFLOW_REPLACE_PATH = @QFLOW_REPLACE_PATH@
QFLOW_NTUPLACE3_PATH = @QFLOW_NTUPLACE3_PATH@
QFLOW_NTUPLACE4_PATH = @QFLOW_NTUPLACE4_PATH@
QFLOW_QROUTER_PATH = @QFLOW_QROUTER_PATH@
QFLOW_MAGIC_PATH = @QFLOW_MAGIC_PATH@
QFLOW_NETGEN_PATH = @QFLOW_NETGEN_PATH@
QFLOW_YOSYS_PATH = @QFLOW_YOSYS_PATH@
QFLOW_OPENTIMER_PATH = @QFLOW_OPENTIMER_PATH@
QFLOW_OPENSTA_PATH = @QFLOW_OPENSTA_PATH@

HAVE_YOSYS = @HAVE_YOSYS@
HAVE_MAGIC = @HAVE_MAGIC@
HAVE_NETGEN = @HAVE_NETGEN@
HAVE_OPENTIMER = @HAVE_OPENTIMER@
HAVE_OPENSTA = @HAVE_OPENSTA@
HAVE_GRAYWOLF = @HAVE_GRAYWOLF@
HAVE_REPLACE = @HAVE_REPLACE@
HAVE_QROUTER = @HAVE_QROUTER@

OBJECTS = vlog2Spice.o vlog2Verilog.o vlog2Def.o vlog2Cel.o vlogFanout.o
OBJECTS += DEF2Verilog.o addspacers.o
OBJECTS += vesta.o spice2delay.o rc2dly.o
OBJECTS += blif2BSpice.o blif2Verilog.o blifFanout.o
HASHLIB = hash.o
LIBERTYLIB = readliberty.o
VERILOGLIB = readverilog.o
LEFLIB = readlef.o
DEFLIB = readdef.o
SOURCES := $(patsubst %.o,%.c,$(OBJECTS))
TARGETS := $(patsubst %.o,%$(EXEEXT),$(OBJECTS))

BININSTALL = ${QFLOW_LIB_DIR}/bin
INSTALL_BININSTALL = @DIST_DIR@/share/qflow/bin

all: $(TARGETS)

vlog2Spice$(EXEEXT): vlog2Spice.o $(HASHLIB) $(VERILOGLIB)
	$(CC) $(LDFLAGS) vlog2Spice.o $(HASHLIB) $(VERILOGLIB) -o $@ $(LIBS)

vlog2Verilog$(EXEEXT): vlog2Verilog.o $(HASHLIB) $(LEFLIB) $(VERILOGLIB)
	$(CC) $(LDFLAGS) vlog2Verilog.o $(HASHLIB) $(VERILOGLIB) $(LEFLIB) \
		-o $@ $(LIBS)

vlog2Cel$(EXEEXT): vlog2Cel.o $(HASHLIB) $(LEFLIB) $(VERILOGLIB)
	$(CC) $(LDFLAGS) vlog2Cel.o $(HASHLIB) $(VERILOGLIB) $(LEFLIB) \
		-o $@ $(LIBS) -lm

vlog2Def$(EXEEXT): vlog2Def.o $(HASHLIB) $(LEFLIB) $(VERILOGLIB)
	$(CC) $(LDFLAGS) vlog2Def.o $(HASHLIB) $(LEFLIB) $(VERILOGLIB) -o $@ $(LIBS) -lm

vlogFanout$(EXEEXT): vlogFanout.o $(HASHLIB) $(LIBERTYLIB) $(VERILOGLIB)
	$(CC) $(LDFLAGS) vlogFanout.o $(HASHLIB) $(VERILOGLIB) $(LIBERTYLIB) \
		-DQFLOW_VERSION=\"VERSION\" -DQFLOW_REVISION=\"REVISION\" \
		$(DEPENDS) -o $@ $(LIBS) -lm

DEF2Verilog$(EXEEXT): DEF2Verilog.o $(HASHLIB) $(VERILOGLIB) $(DEFLIB) $(LEFLIB)
	$(CC) $(LDFLAGS) DEF2Verilog.o $(HASHLIB) $(VERILOGLIB) $(DEFLIB) $(LEFLIB) \
		-DQFLOW_VERSION=\"VERSION\" -DQFLOW_REVISION=\"REVISION\" \
		$(DEPENDS) -o $@ $(LIBS) -lm

addspacers$(EXEEXT): addspacers.o $(HASHLIB) $(LEFLIB) $(DEFLIB)
	$(CC) $(LDFLAGS) addspacers.o $(HASHLIB) $(LEFLIB) $(DEFLIB) -o $@ $(LIBS) -lm

blif2BSpice$(EXEEXT): blif2BSpice.o
	$(CC) $(LDFLAGS) blif2BSpice.o -o $@ $(LIBS)

blif2Verilog$(EXEEXT): blif2Verilog.o
	$(CC) $(LDFLAGS) blif2Verilog.o -o $@ $(LIBS)

blifFanout$(EXEEXT): blifFanout.o $(HASHLIB) $(LIBERTYLIB)
	$(CC) $(LDFLAGS) blifFanout.o $(HASHLIB) $(LIBERTYLIB) \
		-DQFLOW_VERSION=\"VERSION\" -DQFLOW_REVISION=\"REVISION\" \
		$(DEPENDS) -o $@ $(LIBS) -lm

vesta$(EXEEXT): vesta.o $(HASHLIB) $(VERILOGLIB)
	$(CC) $(LDFLAGS) vesta.o $(HASHLIB) $(VERILOGLIB) -o $@ $(LIBS)

spice2delay$(EXEEXT): spice2delay.o $(HASHLIB) $(LIBERTYLIB)
	$(CC) $(LDFLAGS) spice2delay.o $(HASHLIB) $(LIBERTYLIB) -o $@ $(LIBS)

rc2dly$(EXEEXT): rc2dly.o $(LIBERTYLIB) $(HASHLIB) $(VERILOGLIB)
	$(CC) $(LDFLAGS) rc2dly.o $(LIBERTYLIB) $(HASHLIB) $(VERILOGLIB) -o $@ $(LIBS)

install: $(TARGETS)
	@echo "Installing verilog, SPICE, etc. file format handlers"
	$(INSTALL) -d $(DESTDIR)${INSTALL_BININSTALL}
	@for target in $(TARGETS); do \
	   $(INSTALL) $$target $(DESTDIR)${INSTALL_BININSTALL} ;\
	done
	@echo "Installing links to third-party synthesis flow tool executables"
	@if test "${HAVE_GRAYWOLF}" = "1"; then \
	(cd $(DESTDIR)${INSTALL_BININSTALL};  $(RM) -f graywolf;  ln -s $(QFLOW_GRAYWOLF_PATH) graywolf) ;\
	fi
	@if test "${HAVE_REPLACE}" = "1"; then \
	(cd $(DESTDIR)${INSTALL_BININSTALL};  $(RM) -f RePlAce;  ln -s $(QFLOW_REPLACE_PATH) RePlAce) ;\
	(cd $(DESTDIR)${INSTALL_BININSTALL};  $(RM) -f ntuplace3;  ln -s $(QFLOW_NTUPLACE3_PATH) ntuplace3) ;\
	(cd $(DESTDIR)${INSTALL_BININSTALL};  $(RM) -f ntuplace4h;  ln -s $(QFLOW_NTUPLACE4_PATH) ntuplace4h) ;\
	fi
	@if test "${HAVE_QROUTER}" = "1"; then \
	(cd $(DESTDIR)${INSTALL_BININSTALL};  $(RM) -f qrouter;  ln -s $(QFLOW_QROUTER_PATH) qrouter) ;\
	fi
	@if test "${HAVE_MAGIC}" = "1"; then \
	(cd $(DESTDIR)${INSTALL_BININSTALL};  $(RM) -f magic;  ln -s $(QFLOW_MAGIC_PATH) magic) ;\
	fi
	@if test "${HAVE_NETGEN}" = "1"; then \
	(cd $(DESTDIR)${INSTALL_BININSTALL};  $(RM) -f netgen;  ln -s $(QFLOW_NETGEN_PATH) netgen); \
	fi
	@if test "${HAVE_YOSYS}" = "1"; then \
	(cd $(DESTDIR)${INSTALL_BININSTALL};  $(RM) -f yosys;  ln -s $(QFLOW_YOSYS_PATH) yosys); \
	(cd $(DESTDIR)${INSTALL_BININSTALL};  $(RM) -f yosys-abc;  ln -s $(QFLOW_YOSYS_PATH)-abc yosys-abc); \
	fi
	@if test "${HAVE_OPENTIMER}" = "1"; then \
	(cd $(DESTDIR)${INSTALL_BININSTALL};  $(RM) -f ot-shell;  ln -s $(QFLOW_OPENTIMER_PATH) ot-shell); \
	fi
	@if test "${HAVE_OPENSTA}" = "1"; then \
	(cd $(DESTDIR)${INSTALL_BININSTALL};  $(RM) -f sta;  ln -s $(QFLOW_OPENSTA_PATH) sta); \
	fi

uninstall:
	$(RM) -rf ${INSTALL_BININSTALL}

clean:
	$(RM) -f $(OBJECTS) $(HASHLIB) $(LIBERTYLIB) $(LEFLIB) $(DEFLIB) $(VERILOGLIB)
	$(RM) -f $(TARGETS)

veryclean:
	$(RM) -f $(OBJECTS) $(HASHLIB) $(LIBERTYLIB) $(LEFLIB) $(DEFLIB) $(VERILOGLIB)
	$(RM) -f $(TARGETS)

distclean:
	$(RM) -f $(OBJECTS) $(HASHLIB) $(LIBERTYLIB) $(LEFLIB) $(DEFLIB) $(VERILOGLIB)
	$(RM) -f $(TARGETS)

.c.o:
	$(CC) $(CFLAGS) $(CPPFLAGS) $(DEFS) -c $< -o $@
